package com.sinsz.wxpn.open.menu.support;

import com.sinsz.common.exception.ApiException;
import com.sinsz.wxpn.support.Constant;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单参数配置
 * @author chenjianbo
 * @date 2018-11-15
 */
public class MenuInfo {

    private void check() {
        if (this.level < 1 || this.level > 2) {
            throw new ApiException(Constant.DEFAULT_ERRORCODE, "菜单层级范围为[1,2]");
        }
        if (StringUtils.isEmpty(this.name)) {
            throw new ApiException(Constant.DEFAULT_ERRORCODE, "菜单名称不能为空");
        }
        switch (this.level) {
            case 1:
                if (this.name.length() > 4) {
                    throw new ApiException(Constant.DEFAULT_ERRORCODE, "当前层级菜单名称限定4个字");
                }
                break;
            case 2:
            default:
                if (this.name.length() > 7) {
                    throw new ApiException(Constant.DEFAULT_ERRORCODE,"当前层级菜单名称限定7个字");
                }
        }
    }

    /**
     * 点击事件类型菜单
     * @param name  菜单名称
     * @param key   事件值
     * @param level 层级
     */
    public MenuInfo(String name, String key, int level) {
        this.level = level;
        this.name = name;
        this.type = MenuType.CLICK.name().toLowerCase();
        this.key = StringUtils.isEmpty(key) ? "0000" : key;
        check();
    }


    /**
     * 菜单列表，当未设置子列表则默认为点击事件
     * @param name          菜单名称
     * @param sub_button    子菜单列表
     */
    public MenuInfo(String name, List<MenuInfo> sub_button) {
        this.level = 1;
        this.name = name;
        if (sub_button == null || sub_button.isEmpty()) {
            this.type = MenuType.CLICK.name().toLowerCase();
            this.key = "0000";
        } else {
            this.sub_button = sub_button;
            if (this.sub_button.size() > 5){
                throw new ApiException(Constant.DEFAULT_ERRORCODE,"子菜单数量最多支持5个");
            }
        }
        check();
    }

    /**
     * 直接跳转h5页面
     * @param level     层级
     * @param name      菜单名称
     * @param url       跳转链接
     */
    public MenuInfo(int level, String name, String url) {
        this.level = level;
        this.name = name;
        this.type = MenuType.VIEW.name().toLowerCase();
        check();
        this.url = url;
        if (StringUtils.isEmpty(this.url)) {
            throw new ApiException(Constant.DEFAULT_ERRORCODE,"跳转链接不能为空");
        }
    }

    /**
     * 公众号跳转关联小程序
     * @param level         层级
     * @param name          菜单名称
     * @param url           跳转链接
     * @param appid         关联小程序appid
     * @param pagepath      关联小程序中的页面地址
     */
    public MenuInfo(int level, String name, String url, String appid, String pagepath) {
        this.level = level;
        this.name = name;
        this.type = MenuType.MINIPROGRAM.name().toLowerCase();
        check();
        this.url = url;
        if (StringUtils.isEmpty(this.url)) {
            throw new ApiException(Constant.DEFAULT_ERRORCODE,"跳转链接不能为空");
        }
        this.appid = appid;
        this.pagepath = pagepath;
        if (StringUtils.isEmpty(this.appid) || StringUtils.isEmpty(this.pagepath)) {
            throw new ApiException(Constant.DEFAULT_ERRORCODE,"与公众号关联的小程序参数不能为空");
        }
    }

    /**
     * 菜单层级
     * 限定输入1 / 2
     */
    private int level;

    private String type;

    private String name;

    private String key;

    private String url;

    private String appid;

    private String pagepath;

    private List<MenuInfo> sub_button = new ArrayList<>();

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getPagepath() {
        return pagepath;
    }

    public void setPagepath(String pagepath) {
        this.pagepath = pagepath;
    }

    public List<MenuInfo> getSub_button() {
        return sub_button;
    }

    public void setSub_button(List<MenuInfo> sub_button) {
        this.sub_button = sub_button;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }
}
